using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PoolRulesMission : PoolRulesBase
{
	protected MissionPlayer m_Player;

	protected IList<int> m_TargetBalls = new List<int>();

	protected int m_ScoreThisRound;

	protected int m_PunishmentCountThisRound;

	protected int m_RewardCountThisRound;

	private void Awake()
	{
		SingularityBall.SingularityBreakBall = (Delegate1Args<PoolBall>)Delegate.Combine(SingularityBall.SingularityBreakBall, new Delegate1Args<PoolBall>(OnSingularityBreakBall));
	}

	private void OnDestroy()
	{
		SingularityBall.SingularityBreakBall = (Delegate1Args<PoolBall>)Delegate.Remove(SingularityBall.SingularityBreakBall, new Delegate1Args<PoolBall>(OnSingularityBreakBall));
	}

	protected override void Start()
	{
		Pools.DisableStandardBalls();
		List<LevelData.BallData> ballDatas = LevelDataIndex.CurrentLevel.ballDatas;
		Pools.poolTable.SetSurfaceColor(ConstantData.LevelDatas.GetChapter(LevelDataIndex.CurrentLevel.FileName) - 1);
		Transform transform = GameObject.Find("8Ball/OtherObjects").transform;
		int i = 0;
		for (int count = ballDatas.Count; i < count; i++)
		{
			LevelData.BallData ballData = ballDatas[i];
			GameObject original = Resources.Load(ballData.Type.ToString()) as GameObject;
			GameObject gameObject = UnityEngine.Object.Instantiate(original);
			gameObject.transform.SetParent(transform);
			ballData.Position.y = 0f;
			gameObject.transform.position = ballData.Position;
			PoolBall component = gameObject.GetComponent<PoolBall>();
			component.SetBallID(ballData.ID);
			component.ballType = ballData.Type;
			if (component.ballType != BallType.JIANGYOU && component.ballType != BallType.DEMON)
			{
				m_TargetBalls.Add(ballData.ID);
			}
			Pools.CustomBalls.Add(component.GetBallID(), component);
		}
		Pools.CueBall.transform.position = LevelDataIndex.CurrentLevel.cueBallData.Position;
		PocketTrigger.MarkPocketType(LevelDataIndex.CurrentLevel.StartPunishmentPocket, LevelDataIndex.CurrentLevel.StartRewardPocket);
		PocketTrigger.Block(LevelDataIndex.CurrentLevel.BlockPockets);
		m_Player.ShotsRemain = LevelDataIndex.CurrentLevel.shotCount;
		TurnBegin();
		string fileName = LevelDataIndex.CurrentLevel.FileName;
		if (fileName.CompareTo("1-1") == 0)
		{
			RectTransform component2 = SupportTools.AddChild<PoolTutorial>(GameManager.CurrentUIRoot.gameObject, "Tutorial/Tutorial").GetComponent<RectTransform>();
			component2.SetSiblingIndex(component2.GetSiblingIndex() - 1);
			component2.offsetMin = new Vector2(0f, 0f);
			component2.offsetMax = new Vector2(0f, 0f);
		}
		else if (fileName.CompareTo("2-1") == 0)
		{
			RectTransform component3 = SupportTools.AddChild<MasseTutorial>(GameManager.CurrentUIRoot.gameObject, "Tutorial/SidingTutorial").GetComponent<RectTransform>();
			component3.SetSiblingIndex(component3.GetSiblingIndex() - 1);
			component3.offsetMin = new Vector2(0f, 0f);
			component3.offsetMax = new Vector2(0f, 0f);
		}
		else
		{
			FloatTip.Show(LevelDataIndex.CurrentLevel.DescriptionID);
		}
	}

	protected override IEnumerator CheckResultAndChangeTurn(float time)
	{
		yield return new WaitForSeconds(time);
		if (m_PottedBallListThisRound.Count == 0)
		{
			m_Player.Combo = 0;
		}
		if (m_WhiteBallPotted)
		{
			m_Player.Score -= m_ScoreThisRound;
			BaseUIController.text.Show(string.Format(HOLocalizationConfiguration.GetValue(106), m_ScoreThisRound));
			yield return new WaitForSeconds(2f);
			int i = 0;
			for (int count = m_PottedBallListThisRound.Count; i < count; i++)
			{
				m_PottedBallListThisRound[i].BackToPrevRoundState();
				m_TargetBalls.Add(m_PottedBallListThisRound[i].GetBallID());
			}
		}
		else
		{
			int j = 0;
			for (int count2 = m_PottedBallListThisRound.Count; j < count2; j++)
			{
				PoolBall poolBall = m_PottedBallListThisRound[j];
				m_PottedBallList.Add(poolBall.GetBallID(), poolBall);
			}
		}
		m_PottedBallListThisRound.Clear();
		if (CheckGameOver())
		{
			base.State = GlobalState.GAMEOVER;
			if (PoolRulesBase.onGameOver != null)
			{
				if (m_TargetBalls.Count == 0)
				{
					PoolRulesBase.onGameOver(m_Player);
				}
				else if (m_Player.ShotsRemain == 0)
				{
					PoolRulesBase.onGameOver(null);
				}
			}
		}
		else
		{
			TurnBegin();
		}
	}

	protected override void TurnBegin()
	{
		base.TurnBegin();
		m_PunishmentCountThisRound = 0;
		m_RewardCountThisRound = 0;
		if (m_Turn <= 1 || !LevelDataIndex.CurrentLevel.SpecificPocket)
		{
			return;
		}
		PocketIndexes pocketIndexes = PocketIndexes.None;
		if (UnityEngine.Random.Range(0, 100) > 30)
		{
			pocketIndexes = (PocketIndexes)((int)pocketIndexes | (1 << UnityEngine.Random.Range(0, 5)));
			if (UnityEngine.Random.Range(0, 1) == 1)
			{
				pocketIndexes = (PocketIndexes)((int)pocketIndexes | (1 << UnityEngine.Random.Range(0, 5)));
			}
		}
		PocketIndexes pocketIndexes2 = PocketIndexes.None;
		if (UnityEngine.Random.Range(0, 100) > 30)
		{
			pocketIndexes2 = (PocketIndexes)((int)pocketIndexes2 | (1 << UnityEngine.Random.Range(0, 5)));
			if (UnityEngine.Random.Range(0, 1) == 1)
			{
				pocketIndexes2 = (PocketIndexes)((int)pocketIndexes2 | (1 << UnityEngine.Random.Range(0, 5)));
			}
		}
		PocketTrigger.MarkPocketType(pocketIndexes, pocketIndexes2);
	}

	public override void SetPlayers(params IPlayer[] players)
	{
		m_Player = (players[0] as MissionPlayer);
	}

	public override void PotBall(PoolBall ball, PocketIndexes pocket)
	{
		base.PotBall(ball, pocket);
		if ((pocket & PocketTrigger.PunitivePocket) != 0)
		{
			m_Player.AddCues(-2, PocketTrigger.GetPocketWithIndexes(pocket));
		}
		if ((pocket & PocketTrigger.RewardPocket) != 0)
		{
			m_Player.AddCues(1, PocketTrigger.GetPocketWithIndexes(pocket));
		}
		int num = 0;
		if (ball.ballType == BallType.WHITE)
		{
			m_Player.Combo = 0;
			m_Player.AddCues(-2, PocketTrigger.GetPocketWithIndexes(pocket));
			return;
		}
		if (ball.ballType == BallType.BOMB)
		{
			num = 300;
		}
		else if (ball.ballType == BallType.SINGULARITY)
		{
			num = 300;
		}
		else if (ball.ballType == BallType.ABSORB)
		{
			num = 0;
			AbsorbBall absorbBall = (AbsorbBall)ball;
			int i = 0;
			for (int count = absorbBall.AbsorbList.Count; i < count; i++)
			{
				PotBall(absorbBall.AbsorbList[i], pocket);
				Pools.StorageRack.Add(absorbBall.AbsorbList[i]);
			}
			absorbBall.AbsorbList.Clear();
		}
		else if (ball.ballType == BallType.REDCUSTOM)
		{
			num = 100;
		}
		else if (ball.ballType == BallType.BLUECUSTOM)
		{
			num = 200;
		}
		else if (ball.ballType == BallType.YELLOWCUSTOM)
		{
			num = 150;
		}
		else if (ball.ballType == BallType.JIANGYOU)
		{
			num = 0;
		}
		else if (ball.ballType == BallType.DEMON)
		{
			num = 0;
		}
		if (num > 0 && (pocket & PocketTrigger.PunitivePocket) == PocketIndexes.None)
		{
			m_Player.Combo++;
			m_Player.AddScore(num, PocketTrigger.GetPocketWithIndexes(pocket));
			m_ScoreThisRound = num;
		}
		if (m_Player.Combo >= 5)
		{
			BaseUIController.GenerateTips("Great!");
		}
		m_Player.Pot();
		RemoveBall(ball.GetBallID());
	}

	public void RemoveBall(int ballID)
	{
		if (m_TargetBalls.Contains(ballID))
		{
			m_TargetBalls.Remove(ballID);
		}
	}

	protected void OnSingularityBreakBall(PoolBall ball)
	{
		if ((bool)ball)
		{
			UnityEngine.Object.Destroy(ball.gameObject);
			m_TargetBalls.Remove(ball.GetBallID());
		}
	}

	protected override void CustomUpdate()
	{
	}

	protected override void CallPocket()
	{
	}

	protected override bool HandleFouls()
	{
		return false;
	}

	public override bool CheckGameOver()
	{
		return m_TargetBalls.Count == 0 || m_Player.ShotsRemain == 0;
	}

	public override void CueBallHitRail()
	{
	}

	public override void Continue()
	{
		string text = string.Format(HOLocalizationConfiguration.GetValue(342), 2);
		OnContinueCallback();
	}

	private void OnContinueCallback()
	{
		m_Player.Continue();
		base.State = GlobalState.IDLE;
		TurnBegin();
	}
}
